<?php

/**
 * This is the model class for table "tbl_article".
 *
 * The followings are the available columns in table 'tbl_article':
 * @property integer $id
 * @property string $type
 * @property integer $pid
 * @property integer $category_id
 * @property string $title
 * @property string $date_created
 * @property string $desc
 * @property string $content
 * @property integer $is_active
 * @property integer $author_id
 *
 * The followings are the available model relations:
 * @property TblArtCategory $category
 * @property TblUser $author
 */
class Article extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Article the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_article';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('title, date_created, author_id', 'required'),
			array('pid, category_id, is_active, author_id, is_left', 'numerical', 'integerOnly'=>true),
			array('type', 'length', 'max'=>8),
			array('title, desc', 'length', 'max'=>255),
			array('content', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, type, pid, category_id, title, date_created, desc, content, is_active, author_id, is_left', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'category' => array(self::BELONGS_TO, 'TblArtCategory', 'category_id'),
			'author' => array(self::BELONGS_TO, 'TblUser', 'author_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'type' => 'Type',
			'pid' => 'Pid',
			'category_id' => 'Категория',
			'title' => 'Заголовок',
			'date_created' => 'Дата публикации',
			'desc' => 'Краткое описание',
			'content' => 'Текст',
			'is_active' => 'Активность',
			'author_id' => 'Автор',
            'is_left' => 'В левом меню',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('type',$this->type,true);
		$criteria->compare('pid',$this->pid);
		$criteria->compare('category_id',$this->category_id);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('date_created',$this->date_created,true);
		$criteria->compare('desc',$this->desc,true);
		$criteria->compare('content',$this->content,true);
		$criteria->compare('is_active',$this->is_active);
		$criteria->compare('author_id',$this->author_id);
        $criteria->compare('is_left',$this->is_left);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
    public function behaviors()
    	{
    		return array(
                            //'published'=>array('class'=>'ext.behaviors.PublishedBehavior'),

                            'savefiles' => array(
                                'class' => 'pictures.components.SaveFileBehavior',
                                'savelocation' => 'article',
                                'entity' => 'Article',
                                'oneImage'=>true
                            ),
                           /* 'last'=>array('class'=>'ext.behaviors.GetLastBehavior'),
                            'moderationSave'=>array('class'=>'moderation.components.SaveModerationBehavior'),
                            'moderationPublished'=>array('class'=>'moderation.components.PublishedModerationBehavior'),*/

    				);
    	}

    static function normalDate($date){
        return date('d.m.Y', strtotime($date));
    }
}